package controleur;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import actions.ActionConnexion;
import actions.ActionHistorique;
import actions.ActionPanier;
import actions.ActionProduits;
import actions.IAction;

/**
 * Main controler of the application
 * 
 * @author cardon
 * 
 */
public class ControleurServlet extends HttpServlet {
	private HashMap<String, IAction> lesActions;
	private ServletContext sc;


	public void init(ServletConfig config) {
		try {
			super.init(config);
			sc = config.getServletContext();
			initActions();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void initActions() {
		lesActions = new HashMap<String, IAction>();
		lesActions.put("default", new ActionProduits());
		lesActions.put("panier", new ActionPanier());
		lesActions.put("produits", new ActionProduits());
		lesActions.put("historique", new ActionHistorique());
		lesActions.put("connexion", new ActionConnexion());
	}

	private IAction getAction(String cmd) throws Exception {
		if (cmd == null)
			cmd = "default";
		IAction action = lesActions.get(cmd);
		if (action == null)
			throw new Exception("Commande introuvable");
		return action;
	}

	private String executeAction(String actionName, HttpServletRequest req) {
		String next;
		IAction action = null;
		try {
			action = getAction(actionName);
			next = action.execute(req);
		} catch (Exception e) {
			req.setAttribute("erreur", e.getMessage());
			next = "erreur.jsp";
			e.printStackTrace();
		}

		return next;
	}

	public void doGet(HttpServletRequest req, HttpServletResponse rep) {
		
		RequestDispatcher rd = null;
		int redirectCount = 0;
		String next = req.getParameter("cmd");	
		

		// /!\ CAREFUL => redirect loop /!\
		do {
			next = this.executeAction(next, req);
			redirectCount++;
		} while ((!next.contains(".jsp")) && (redirectCount < 5));
		// too many redirections
		if (redirectCount == 4) {
			next = "erreur.jsp";
		}
		redirectCount = 0;

		// action gives a jsp name to be displayed
		rd = sc.getRequestDispatcher("/vues/" + next);

		try {
			rd.forward(req, rep);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest req, HttpServletResponse rep) {
		RequestDispatcher rd = null;
		int redirectCount = 0;
		String next = req.getParameter("cmd");	

		// /!\ CAREFUL => redirect loop /!\
		do {
			next = this.executeAction(next, req);
			redirectCount++;
		} while ((!next.contains(".jsp")) && (redirectCount < 5));
		// too many redirections
		if (redirectCount == 4) {
			next = "erreur.jsp";
		}
		redirectCount = 0;

		// action gives a jsp name to be displayed
		rd = sc.getRequestDispatcher("/vues/" + next);

		try {
			rd.forward(req, rep);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}